<?php
namespace app\trade\controller;
use think\facade\Db;
class Test
{

    // 规上服务业营业收入统计
    private function index() {
        $post['year'] = input('post.year','2022');
        $post['month'] = input('post.month','3');
        $post['trade_id'] = input('post.trade_id','1');
        try {
            checkPost($post);
            $month = intval($post['month']);
            $trade_id = intval($post['trade_id']);
            if(($month < 2 || $month > 12) && $month!==0) {
                return ajax('',-1,'无效的参数 month');
            }
            if(!in_array($trade_id,[1,2,3])) {
                return ajax('',-1,'无效的参数 trade');
            }
            // 获取全部区县
            $area_list = Db::table('area_data')->field('area')->group('area')->select()->toArray();
            $where = [];
            $list = Db::table('area_data')->where($where)->select()->toArray();
            foreach ($area_list as &$area) {
                $area['income'] = 0;
                $area['income_last_year'] = 0;
                $area['rate'] = 0;
                foreach ($list as $row) {
                    if($row['trade'] === $trade_id && $row['area'] === $area['area']) {
                        if($month > 0) {
                            $area['income'] += $row['month'.$month];
                            $area['income_last_year'] += $row['month'.$month.'_last_year'];
                        }else {
                            for($i=2;$i<=12;$i++) {
                                $area['income'] += $row['month'.$i];
                                $area['income_last_year'] += $row['month'.$i.'_last_year'];
                            }
                        }
                    }
                }
                $area['rate'] = $area['income_last_year'] > 0 ? round(($area['income']-$area['income_last_year'])/$area['income_last_year'],4) : 0;

            }
        } catch (\Exception $e) {
            return ajax($post,-1,$e->getMessage());
        }
        pp($area_list);
    }

    public function synData() {
        $arr = [];
        for ($month=1;$month<=12;$month++) {
            $arr['area_data'.$month] = Db::table('area_data'.$month)->select()->toArray();
        }
        $list = Db::table('area_data')->where('year','=',2022)->select()->toArray();
        $affect_rows = 0;
        foreach ($list as $row) {
            $update_data = [
                'company_num' => 0,
                'month1' => 0,
                'month1_last_year' => 0,
                'month1_rate' => 0,
                'month2' => 0,
                'month2_last_year' => 0,
                'month2_rate' => 0,
                'month3' => 0,
                'month3_last_year' => 0,
                'month3_rate' => 0,
                'month4' => 0,
                'month4_last_year' => 0,
                'month4_rate' => 0,
                'month5' => 0,
                'month5_last_year' => 0,
                'month5_rate' => 0,
                'month6' => 0,
                'month6_last_year' => 0,
                'month6_rate' => 0,
                'month7' => 0,
                'month7_last_year' => 0,
                'month7_rate' => 0,
                'month8' => 0,
                'month8_last_year' => 0,
                'month8_rate' => 0,
                'month9' => 0,
                'month9_last_year' => 0,
                'month9_rate' => 0,
                'month10' => 0,
                'month10_last_year' => 0,
                'month10_rate' => 0,
                'month11' => 0,
                'month11_last_year' => 0,
                'month11_rate' => 0,
                'month12' => 0,
                'month12_last_year' => 0,
                'month12_rate' => 0,
            ];
            foreach ($arr as $table_name=>$table) {
                foreach ($table as $table_row) {
                    $trade = $row['trade']; //分区
                    for($i=1;$i<=12;$i++) {
                        if($table_name === 'area_data'.$i) {
                            if($table_row['area'] === $row['area']) {
                                $update_data['company_num'] = $table_row['company_num_'.$trade];
                                $update_data['month'.$i] = $table_row['month_data_'.$trade];
                                $update_data['month'.$i.'_last_year'] = $table_row['month_data_last_year_'.$trade];
                                $update_data['month'.$i.'_rate'] = $table_row['rate_'.$trade];
                            }
                        }
                    }
                }
            }
            $row = Db::table('area_data')->where('id','=',$row['id'])->update($update_data);
            $affect_rows += $row;
        }
        pp('SUCCESS ' .$affect_rows);
    }

    // 空的补0
    public function fillZero() {
        for ($month=1; $month<=12; $month++) {
            Db::table('area_data'.$month)->where('company_num_1 is null')->update(['company_num_1'=>0]);
            Db::table('area_data'.$month)->where('month_data_1 is null')->update(['month_data_1'=>0]);
            Db::table('area_data'.$month)->where('month_data_last_year_1 is null')->update(['month_data_last_year_1'=>0]);
            Db::table('area_data'.$month)->where('rate_1 is null')->update(['rate_1'=>0]);

            Db::table('area_data'.$month)->where('company_num_2 is null')->update(['company_num_2'=>0]);
            Db::table('area_data'.$month)->where('month_data_2 is null')->update(['month_data_2'=>0]);
            Db::table('area_data'.$month)->where('month_data_last_year_2 is null')->update(['month_data_last_year_2'=>0]);
            Db::table('area_data'.$month)->where('rate_2 is null')->update(['rate_2'=>0]);

            Db::table('area_data'.$month)->where('company_num_3 is null')->update(['company_num_3'=>0]);
            Db::table('area_data'.$month)->where('month_data_3 is null')->update(['month_data_3'=>0]);
            Db::table('area_data'.$month)->where('month_data_last_year_3 is null')->update(['month_data_last_year_3'=>0]);
            Db::table('area_data'.$month)->where('rate_3 is null')->update(['rate_3'=>0]);

        }
    }

    // 初始化数据
    private function initData()
    {
        for($i=4;$i<=14;$i++) {
            Db::table('area_data2')->insert(['company_num_1'=>0]);
            Db::table('area_data3')->insert(['company_num_1'=>0]);
            Db::table('area_data5')->insert(['company_num_1'=>0]);
            Db::table('area_data6')->insert(['company_num_1'=>0]);
            Db::table('area_data7')->insert(['company_num_1'=>0]);
            Db::table('area_data8')->insert(['company_num_1'=>0]);
            Db::table('area_data10')->insert(['company_num_1'=>0]);
            Db::table('area_data11')->insert(['company_num_1'=>0]);
        }
        for($i=3;$i<=14;$i++) {
            Db::table('area_data4')->insert(['company_num_1'=>0]);
            Db::table('area_data9')->insert(['company_num_1'=>0]);
        }
        pp('success');
    }

}
